<?php
class Control_ServerList_saveServer {
 	function saveServer() {
 		$s_id = MooForm::request('s_id');
 		
 		$id 	= MooForm::request('id');
 		$name 	= MooForm::request('name');
 		$address = MooForm::request('address');
 		$port	= MooForm::request('port');
 		$state	= MooForm::request('state');
 		$order 	= MooForm::request('order');
 		
 		if(empty($s_id)) {
 			$return['code'] = 2;
 			$return['msg']  = "保存失败,s_id不存在";
 			$this->backMsg($return);
 		}

 		$ServerlistDao = MooDao::get('Serverlist');
 		$ServerlistObj = $ServerlistDao->load($s_id);
 		if(!$ServerlistObj) {
 			$return['code'] = 3;
 			$return['msg']  = "保存失败,未s_id不存在";
 			$this->backMsg($return);
 		}
 		
 		$serverlistCfg 	= $ServerlistObj->s_serverlist;
 		$fileName 		= $ServerlistObj->s_filename;
 		$game 			= $ServerlistObj->s_game;
 		$platform 		= $ServerlistObj->s_platform;
 		
 		// 初始化 第一个
 		if(empty($serverlistCfg)) {
 			$serverListNew = array();
 			$serverListNew['version'] = 1;
 			$serverListNew['defaultSeverID'] = 1;
 			// 第一个服列表
 			$server = array(
 					'id' => $id,
 					'name' => $name,
 					'address' => $address,
 					'port' => $port,
 					'state' => $state,
 					'order' => $order,
 			);
 			$serverListNew['severs'][] = $server;
 			$newServerlistInfo = MooJson::encode($serverListNew);
 			
 		} else {
 			// 解析$serverlistCfg
 			$serverlistArr = MooJson::decode($serverlistCfg);
 			$servers = $serverlistArr['severs'];
 		
	 		$servers_ids = array();
	 		foreach ($servers as $key => $val) {
	 			$servers_ids[] = $val['id'];
	 		}
	
	 		// 已有的是修改
	 		if(in_array($id, $servers_ids)) {
	 			foreach ($servers as $key => $val) {
	 				$servers_id = $val['id'];
	 				if($id == $servers_id) {
	 					// 重置
	 					$servers[$key]['name'] = $name;
	 					$servers[$key]['address'] = $address;
	 					$servers[$key]['port'] = $port;
	 					$servers[$key]['state'] = $state;
	 					$servers[$key]['order'] = $order;
	 				}
	 			}
	 			
	 		} else {
	 			$servers[] = array(
	 				'id' => $id,	
	 				'name' => $name,	
	 				'address' => $address,	
	 				'port' => $port,	
	 				'state' => $state,	
	 				'order' => $order,	
	 			);
	 		}
	 		
	 		// servers 重置
	 		$serverlistArr['severs'] = $servers;
	 		$newServerlistInfo = MooJson::encode($serverlistArr);
 		}
 		
 		// 写入数据库与 本地文件即可
 		$ServerlistObj->set('s_serverlist', $newServerlistInfo);
 		
 		$formatPath = ROOT_PATH . "/servistLists";
 		$formatFile = ROOT_PATH . "/servistLists/{$game}_{$platform}_$fileName";
		if(!MooFile::isExists($formatPath)) {
			MooFile::mkDir($formatPath);
		}
		
		// 格式化服列表到文件
		$newServerlistInfo = $this->OBJ->formatServerList($newServerlistInfo);
		// 每次保存 覆盖重写该文件
		$rs = MooFile::write($formatFile, $newServerlistInfo);
 		if(!$rs) {
 			$return['code'] = 4;
 			$return['msg']  = "保存失败,写入文件失败";
 			$this->backMsg($return);
 		}
 		
 		$return['code'] = 1;
 		$return['msg']  = "保存成功";
 		$this->backMsg($return);
 		
	}
	
	private function backMsg($return) {
		exit(json_encode($return));
	}
}
